import random

# 定义一些常量
WIDTH = 30
FILL_CHAR = '#'


# 打印素数
def print_prime_number():
    for num in range(2, 100):
        flag = False
        mid = int(num / 2) + 1
        for div in range(2, mid):
            if num % div == 0:
                flag = True
                continue
        if not flag:
            print(num, end=',')
    print('是素数')


# 打印9*9乘法表
def print_multiplication_table():
    for left in range(1, 10):
        for right in range(1, left + 1):
            print(f'{left}*{right}={left * right}', end=' ')
        print()


# 打印三角形
def print_triangle(level):
    # level 必须是奇数
    if level % 2 == 0:
        level += 1
    # 比 level 大的最小偶数
    min_even = level + 1

    # 超过中间数，需要下次打印需要反转
    mid = int(min_even / 2)

    for index in range(1, min_even):
        if index > mid:
            print('* ' * (min_even - index))
        else:
            print('* ' * index)


# 用 while 循环，实现猜年龄
def guess_age_game(age):
    print("开始猜年龄游戏！")
    flag = True
    while flag:
        guess_age = input("请输入年龄：")
        if guess_age.isdigit():
            # 转为整型
            guess_age = int(guess_age)
        else:
            print("输入有误，请继续！")
            continue
        if guess_age == age:
            flag = False
            print(f"恭喜你，猜对了，年龄为：[{age}]")
        elif guess_age > age:
            print(f"你猜的年龄[{guess_age}]大了!")
        else:
            print(f"你猜的年龄[{guess_age}]小了!")
    print("游戏结束！")


# 年利率0.0325，存款多少年能翻倍
def deposit_doubling(deposit):
    # 翻倍的存款
    double_deposit = deposit * 2
    # 当前存款
    current_deposit = deposit
    # 当前的利率
    interest_rate = 0.0325
    # 需要的年份
    year = 0
    while current_deposit <= double_deposit:
        year += 1
        current_deposit += current_deposit * interest_rate
    else:
        print(f"存款需要{year}年才能翻倍")


# 小球从100m坠落，每次反弹高度为坠落高度的一半，求第n次反弹完后的距离
def ball_drop(height, n):
    drop = height
    i = 0
    total = 0
    while i < n:
        i += 1
        total += (drop + drop / 2)
        drop = drop / 2
    else:
        print(f"小球反弹{i}次后，共走了{total}米")


# 总共有n个桃子，猴子每次吃一半后再多吃一个，吃了10天，到第11天只剩下一个，问吃之前一共有多少个桃子
def monkey_eat_peach(days):
    if days == 11:
        return 1
    else:
        return (monkey_eat_peach(days + 1) + 1) * 2


# 随机抽奖程序
# 有n名员工，一等奖x个，二等奖y个，三等奖z个

def random_draw(n, x, y, z):
    # 生成n名员工的工号
    staff_list = list(range(1, n + 1))
    print('总共员工：', staff_list)
    # 三等奖员工工号
    third_prize_list = random.sample(staff_list, z)
    print('三等奖员工：', third_prize_list)
    # 每次抽完奖，需要移除这些已经中奖用户
    staff_list = [staff_list[index] for (index, staff) in enumerate(staff_list) if staff not in third_prize_list]
    # print('员工池：', staff_list)
    # 二等奖员工工号
    second_prize_list = random.sample(staff_list, y)
    print('二等奖员工：', second_prize_list)
    # 每次抽完奖，需要移除这些已经中奖用户
    staff_list = [staff_list[index] for (index, staff) in enumerate(staff_list) if staff not in second_prize_list]
    # print('员工池：', staff_list)
    # 一等奖员工工号
    first_prize_list = random.sample(staff_list, x)
    print('一等奖员工：', first_prize_list)
    # print('员工池：', staff_list)
    return {1: first_prize_list, 2: second_prize_list, 3: third_prize_list}


print('打印素数'.center(WIDTH, FILL_CHAR))
print_prime_number()

print('打印9*9乘法表'.center(WIDTH, FILL_CHAR))
print_multiplication_table()

print('打印三角形'.center(WIDTH, FILL_CHAR))
print_triangle(10)

# print('猜年龄小游戏'.center(WIDTH, FILL_CHAR))
# guess_age_game(50)

print('存款翻倍'.center(WIDTH, FILL_CHAR))
deposit_doubling(1000)

print('小球反弹'.center(WIDTH, FILL_CHAR))
ball_drop(100, 10)
ball_drop(100, 100)

print('猴子吃桃，总桃子数'.center(WIDTH, FILL_CHAR))
print(monkey_eat_peach(1))

print('抽奖程序'.center(WIDTH, FILL_CHAR))
print(random_draw(10, 1, 2, 7))
